$(document).ready(function() {
	//下拉框查询
	searchEvent();
	//初始化数据
	init(1);
	
});
	
//默认加载  
function init(pageNo){
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/message/jpa/pageshow?pageNum="+pageNo+"&pageSize="+pageSize+"&userType="+$("#change").val(),
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){	
					var id = n.id;
					var type=typeStr(n.type);
					var thislist = 
					'	<tr>'+				
					'		<td>'+n.id+'</td>'+
					'		<td>'+n.title+'</td>'+		
					'		<td>'+n.gmtDatetime+'</td>'+
					'		<td>'+type+'</td>'+					
					'		<td>'+			
					'			<a class="btn btn-primary btn-xs" href="notice_details.html?id='+id+'">详情</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="deleteeach('+id+')"> 删除</a>'+
					'			<a class="btn btn-primary btn-xs" href="notice_add.html?id='+id+'"> 修改</a>'+	
					'		</td>'+
					'	</tr>';
					$('#thislist').append(thislist);	
														
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
}
//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}

/**
 * 下拉框查询
 */
function searchEvent(){
	$("#change").change(function () {
		init(1);
	});
}
   
//删除
function deleteeach(id){
	if(confirm("确定要删除吗？")) {
		deletegq(id);
	}
}

function deletegq(id){
	$.ajax({
		url: urlcore + "/api/message/deleteMessageOne?messageId="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				init(1);
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

/**
 * 用户状态返回
 * @param {Object} type
 */
function typeStr(type){
	if(type==1) {
		type = '站内消息';
	}
	else if(type==2) {
		type = '手机消息';
	}
	return type;
}